(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.directCharge.listController", listController);
	listController.$inject = [ "$scope", "$q", "gridService", "modalService", "layoutConfig", "validatorConfig", "authService", "directChargeService" ];
	function listController($scope, $q, gridService, modalService, layoutConfig, validatorConfig, authService, directChargeService) {
		var ctrl = this;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, serviceRowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = "vm.searchCriteria.list";
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.dchargeStatusOptions = [ {
				"value" : "L",
				"label" : "option.dchargestatus.logged"
			}, {
				"value" : "R",
				"label" : "option.dchargestatus.released"
			}, {
				"value" : "X",
				"label" : "option.dchargestatus.cancelled"
			} ];
			directChargeService.getCurrencyOptions().then(function(result) {
				return $q(function(resolved, rejected) {
					ctrl.refCcyOptions = result;
					resolved();
				});
			}).then(function() {
				ctrl.search();
			});
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ "title.dialogs.info.norecsel" ]
				});
				return;
			}
			$scope.go("cls.directCharge.view", ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function serviceRowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}
	}
})(angular);
